#! /bin/bash

NOTIFY="/opt/tiger/deploy_script/send_ding.py"

function red(){
    echo -e "\033[0;31;1m$1\033[0m"
}

function green(){
    echo -e "\033[0;32;1m$1\033[0m"
}

function yellow(){
    echo -e "\033[0;33;1m$1\033[0m"
}

COMMON_CTL="./tools/common_ctl.py"


function usage(){
    green "Usage: $1 -c cmd -u submitter [-d] [-f] [-i] [-p] [-s]"
    green "cmd:"
    green "   start: 启动服务"
    green "   stop:  停止服务"
    green "   restart-one: 每个机房启动一个服务实例, 启动各机房机器列表第一个"
    green "   restart-all: 重启所有服务实例"
    green "   restart-all-1: 每个机房保留一台不重启，保留个机房机器列表最后一个"
    green "   restart-kill-all: 发送kill -9信号,针对svc服务"
    green "   status: 输出机器启动状态"
    green "   lock: 锁住服务操作"
    green "   unlock: 解锁被lock的服务服务"
    green "参数说明:"
    green "   -i: 忽略其他人的操作，强制上线"
    green "   -p: 并发操作数，默认为1"
    green "   -f: 忽略小流量上线和重启检查，仅用于紧急情况"
    green "   -s: 机器间重启间隔，默认2s"
    green "   -d: 重启机房, lf or hy, 默认全机房重启"
}

function get_opt(){
    cmd=$1
    if [ "x$cmd" == "xrestart-one" -o "x$cmd" == "xrestart-all" -o "x$cmd" == "xrestart-all-1" ];then
        return
    elif [ "x$cmd" == "xrestart-kill-all" ]; then
        return
    elif [ "x$cmd" == "xstart" -o "x$cmd" == "xstop" -o "x$cmd" == "xstatus" -o "x$cmd" == "xstatus" -o "x$cmd" == "xdeploy" -o "x$cmd" == "xundeploy" -o "x$cmd" == "xis-active" ];then 
        return
    elif [ "x$cmd" == "xlock" ];then
        return
    elif [ "x$cmd" == "xunlock" ];then
        return
    else
        usage ${PROG_NAME}
        exit 1
    fi
}

function main(){
    start_time=`date +%s`
    if [ -z ${PROG_NAME} ]; then
        exit 1
    fi

    if [ $# -lt 4 ]; then
        usage ${PROG_NAME}
        exit 1
    fi
 
    while getopts "c:u:d:s:p:if" arg
#    while getopts "c:p:d:u:qfi" arg
    do
        echo "case $arg"
        case $arg in
            c)
                CMD=$OPTARG
                ;;
            d)
                DC=$OPTARG
                ;;
            u)
                XUSER=$OPTARG
                ;;
            p)
                PARALLEL=$OPTARG
                ;;
            q)
                QUICK=0
                ;;
            f)
                FORCE=0
                ;;
            i)
                IGNORE=0
                ;;
            s)
                SLEEP_INTERVAL=$OPTARG
                ;;
            ?)
                usage ${PROG_NAME}
        exit 1
        ;;
        esac
    done

    if [ "x$CMD" == "xstop" ]; then
        read -p "ATTENTION!! STOP ALL INSTANCES? [y/n]" -n 1 -r
        echo
        if [[ ! $REPLY =~ ^[Yy]$ ]]
        then
            exit 1
        fi
    fi

    if [ -z ${SYSTEMD_SERVICE} ];then
        red "You should export SYSTEMD_SERVICE=your_service.service in your script!!!Only ${cmd} using svc when skip it"
        SYSTEMD_SERVICE="NoneNull"
    fi

    if [ -z ${SVC_RUN_DIR} ];then
        SVC_RUN_DIR=${SYSTEMD_SERVICE}
    fi

    if [ -z ${SLEEP_INTERVAL} ];then
        SLEEP_INTERVAL=2
    fi

    if [ -z ${PROCESS_NUM} ];then
        PROCESS_NUM=0
    fi

    if [ -z ${THREAD_NUM} ];then
        yellow "You should export THREAD_NUM={service_thread_num} in your ctl srcript for servcie check"
        THREAD_NUM=1
    fi

    if [ -z ${PSM} ];then
        red "重启脚本没有指定PSM,请指定PSM,PSM与ms.byted.org中PSM保持一致."
        red "请指定PSM, PSM即模块在ms.byted.org平台上申请的psm, 脚本中通过export PSM={psm}生效"
        red "6月10日以后不指定PSM，重启脚本不生效!!"
        exit -1
    fi

    if [ -z ${SERVICE_PORT} ];then
        SERVICE_PORT=0
    fi

    get_opt ${CMD}

    if [ ! -z ${QUICK} ] && [ -z ${PARALLEL} ];then
        PARALLEL=10
    elif [ -z ${PARALLEL} ];then
        PARALLEL=1
    fi

    should_ignore=""
    if [ ! -z ${IGNORE} ];then
        should_ignore="-i"
    fi
    force=""
    if [ ! -z ${FORCE} ];then
        force="-f"
    fi
    specify_dc=""
    if [ ! -z ${DC} ];then
        specify_dc="-d ${DC}"
    fi

    if test ! -z $CHANNELID;then
        $NOTIFY $CHANNELID $XUSER $CMD $PROG_NAME 0
    fi

    excldue_hosts=""
    if test ! -z $EXCLUDE_HOSTS;then
      exclude_hosts="--exclude_hosts ${EXCLUDE_HOSTS}"
    fi

    ${COMMON_CTL} -u ${XUSER} -c ${CMD} --svc ${SVC_RUN_DIR} --sctl ${SYSTEMD_SERVICE} --hosts ${HOST_LIST} --program ${PROG_NAME} --psm ${PSM} --repo ${REPO} --port ${SERVICE_PORT} --proc_num ${PROCESS_NUM} --thread_num ${THREAD_NUM} --sleep ${SLEEP_INTERVAL} -p ${PARALLEL} ${should_ignore} ${specify_dc} ${force} ${exclude_hosts}

    ret=$?
    if test $ret -ne 0;then
        exit $ret
    fi

    if test ! -z $CHANNELID;then
        $NOTIFY $CHANNELID $XUSER $CMD $PROG_NAME 1
    fi
}

kinit deploy -k -t /home/tiger/.tdeploy_keytab
yellow "重启脚本已经升级，使用问题请联系yuyuntao@bytedance.com"
main $@
python ./submit_timeline.py "$start_time" "$XUSER" "$REPO" "$ARGS"
